<template>
  <Cascader
    :options="options"
    :show-search="true"
    :value="value"
    :placeholder="placeholder"
    v-bind="$attrs"
    v-on="$listeners"
    :fieldNames="fieldNames"
    @change="change"
  />
</template>

<script>
import 'ant-design-vue/lib/cascader/style/index.js'
import { Cascader } from 'ant-design-vue'
import {
  provinceData,
  provinceAndCityData,
  regionData,
  provinceAndCityDataPlus,
  regionDataPlus,
  CodeToText,
  TextToCode,
} from './index'
/**
 *Parser
 *@Author
 *@Version
 *@property
 *@event
 */
export default {
  components: {
    Cascader,
  },
  props: {
    value: {
      type: Array,
      default: () => [],
    },
    // 提示信息
    placeholder: {
      type: String,
      default: '请选择省市区',
    },
    type: {
      type: String,
      default: 'region',
    },
    showAll: {
      type: Boolean,
      default: false,
    },
    // 只显示部分省市区 传省名称
    filter: {
      type: Array,
      default() {
        return []
      },
    },
    fieldNames: {
      type: Object,
      default() {
        return {
          label: 'label',
          value: 'label',
          children: 'children',
        }
      },
    },
  },
  mounted() {
    this.init()
  },
  data() {
    return {
      options: [],
    }
  },
  methods: {
    init() {
      const dataMap = new Map([
        ['province', provinceData],
        ['city', provinceAndCityData],
        ['cityPlus', provinceAndCityDataPlus],
        ['region', regionData],
        ['regionPlus', regionDataPlus],
      ])
      const key = `${this.type}${this.showAll ? 'Plus' : ''}`
      const data = dataMap.get(key)
      if (this.filter.length >= 1) {
        const filterData = []
        this.filter.forEach((lable) => {
          const item = data.find((e) => e[this.fieldNames.label] === lable)
          if (item) {
            filterData.push(item)
          }
        })
        this.options = filterData
      } else {
        const filterDataGui = []
        let GuiYang = false
        data.forEach((lable) => {
          const item = data.forEach((e) => {
            if (e[this.fieldNames.label] === lable.label) {
              if (e[this.fieldNames.label] == '贵州省') {
                for (const key in e[this.fieldNames.children]) {
                  if (e[this.fieldNames.children][key].label == '贵阳市') {
                    e[this.fieldNames.children].splice(key, 1)
                    GuiYang = true
                    filterDataGui.push(e)
                  } else if (GuiYang === false) {
                    GuiYang = true
                    filterDataGui.push(e)
                  }
                }
              } else {
                filterDataGui.push(e)
              }
            }
          })
        })
        this.options = filterDataGui
        // this.options = data
      }
    },
    $CodeToText(e) {
      return CodeToText[e]
    },
    $TextToCode(e) {
      return TextToCode[e]
    },
    change(e) {
      this.$emit('input', e)
      this.$emit('change', e)
    },
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-cascader-picker {
  width: 100%;
}
</style>
